package com.example.musicplayer.activity;

import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.example.musicplayer.R;
import com.example.musicplayer.commonUtils.DisplayUtil;
import com.example.musicplayer.windowTools.WindowTools;

public class ImmersiveActivity extends BaseActivity {
    private View mView;
    private WindowManager.LayoutParams mParams;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        generateLayoutParams();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            WindowTools.hideAll(getWindow());
        }
    }

    @Override
    public void setContentView(int layoutResID) {
        View view = LayoutInflater.from(this).inflate(layoutResID, null);
        setContentView(view);
    }

    @Override
    public void setContentView(View view) {
        if (view == null) {
            setContentView(R.layout.empty_view);
        } else {
            generateLayoutParams();
            mView = view;
            getWindowManager().addView(mView, mParams);
            mView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visibility) {
                    if (visibility == View.SYSTEM_UI_FLAG_VISIBLE) {
                        WindowTools.hideAll(getWindow());
                    }
                }
            });
        }
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        super.setContentView(view);
    }

    @Override
    public <T extends View> T findViewById(int id) {
        if (mView == null) {
            return null;
        }
        return mView.findViewById(id);
    }

    protected void fitsStatusBar(final Toolbar toolbar) {
        if (toolbar != null) {
            toolbar.post(new Runnable() {
                @Override
                public void run() {
                    int statusHeight = DisplayUtil.getStatusBarHeight(getResources());
                    int height = toolbar.getHeight();
                    if (toolbar.getLayoutParams() != null) {
                        toolbar.getLayoutParams().height = height + statusHeight;
                        toolbar.setPadding(toolbar.getPaddingLeft(), toolbar.getPaddingTop() + statusHeight,
                                toolbar.getPaddingRight(), toolbar.getPaddingBottom());
                    }
                }
            });
        }
    }

    private void generateLayoutParams() {
        if (mParams == null) {
            mParams = new WindowManager.LayoutParams();
            mParams.x = 0;
            mParams.y = 0;
            mParams.width = WindowManager.LayoutParams.MATCH_PARENT;
            mParams.height = WindowManager.LayoutParams.MATCH_PARENT;
            mParams.gravity = Gravity.TOP;
            mParams.format = PixelFormat.RGBA_8888;
            mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
        }
    }
}